package com.fand.manager.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import com.fand.common.exception.BusinessException;
import com.fand.common.util.JsonUtils;
import com.fand.common.util.PageResult;
import com.fand.manager.model.operate.SysUserOperateSearchDTO;
import com.fand.manager.service.ISysUserOperateRecordService;
import com.fand.stater.constant.EventCodeEnum;
import com.fand.stater.constant.FandIndexConstant;
import com.fand.stater.model.es.SysUserESIndexRecord;
import com.fand.stater.model.es.SysUserOperateRecord;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service;

/**
 * Author: zhangjiabin
 * Email: javaboyok@163.com
 * CreateDate: 2021/6/27
 * Version: 1.0
 */
@Slf4j
@Service
public class SysUserOperateRecordServiceImpl implements ISysUserOperateRecordService {

    @Resource
    private RestHighLevelClient restHighLevelClient;

    @SneakyThrows
    @Override
    public PageResult<List<SysUserOperateRecord>> list(SysUserOperateSearchDTO sysUserOperateSearchDTO) {
        if(Objects.isNull(sysUserOperateSearchDTO)){
            throw new BusinessException("非法参数");
        }
        List<SysUserOperateRecord> listDatas = CollUtil.newArrayList();

        SearchRequest searchRequest = new SearchRequest(FandIndexConstant.FAND_USER_OPERATE_RECORD + sysUserOperateSearchDTO.getDateTime());
        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource();
        BoolQueryBuilder queryBuilder = new BoolQueryBuilder();

        if(Objects.nonNull(sysUserOperateSearchDTO.getUserCode())){
            queryBuilder.must(QueryBuilders.termQuery("userCode.keyword",sysUserOperateSearchDTO.getUserCode()));
        }
        if(Objects.nonNull(sysUserOperateSearchDTO.getUserName())){
            queryBuilder.must(QueryBuilders.termQuery("userName.keyword",sysUserOperateSearchDTO.getUserName()));
        }
        if(Objects.nonNull(sysUserOperateSearchDTO.getRequestURI())){
            queryBuilder.must(QueryBuilders.termQuery("requestURI.keyword",sysUserOperateSearchDTO.getRequestURI()));
        }

        searchSourceBuilder.from((sysUserOperateSearchDTO.getPage()-1)*sysUserOperateSearchDTO.getLimit()).size(sysUserOperateSearchDTO.getLimit());
        searchSourceBuilder.query(queryBuilder);
        searchSourceBuilder.sort("time", SortOrder.DESC);
        searchRequest.source(searchSourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        SearchHit[] hits = searchResponse.getHits().getHits();
        if(ArrayUtil.isNotEmpty(hits)){
            Arrays.stream(hits).forEach(hit ->{
                SysUserOperateRecord sysUserOperateRecord = JsonUtils.jsonToPojo(hit.getSourceAsString(), SysUserOperateRecord.class);
                listDatas.add(sysUserOperateRecord);
            });
        }
        return new PageResult(Integer.parseInt(String.valueOf(searchResponse.getHits().getTotalHits().value)),listDatas);
    }
}
